import 'package:get/get.dart';
import 'package:live_music/http/http_utils.dart';
import 'package:live_music/models/response/countInfo_response.dart';
import 'package:live_music/models/user_info_model.dart';
import 'package:live_music/models/user_setting_model.dart';
import 'package:live_music/services/userService.dart';
import 'package:live_music/utils/toast_utils.dart';

class UserInfoController extends GetxController {
  final userInfo = UserInfoModel().obs;
  final countInfo = CountInfoResponse().obs;
  final userSetting = UserSettingModel().obs;
  void getUserInfo() async {
    try {
      Map<String, dynamic> result = await UserService.getUserInfo();
      userInfo.value = UserInfoModel.fromJson(result['body']);
    } catch (error) {
      ToastUtils.showToast(error['retdesc']);
    }
  }

  void loadCountInfo() async {
    try {
      Map<String, dynamic> data = {};
      Map<String, dynamic> result = await UserService.getCountInfo(data);
      countInfo.value = CountInfoResponse.fromJson(result['body']);
    } catch (error) {
      ToastUtils.showToast(error['retdesc']);
    }
  }

  void loadUserSetting() async {
    try {
      Map<String, dynamic> data = {};
      Map<String, dynamic> result = await UserService.getUserSetting(data);
      userSetting.value = UserSettingModel.fromJson(result['body']);
    } catch (error) {
      ToastUtils.showToast(error['retdesc']);
    }
  }

  void updateUserSetting(data) async {
    try {
      print(data);
      await UserService.updateUserSetting(data);
      loadUserSetting();
    } catch (error) {
      ToastUtils.showToast(error['retdesc']);
    }
  }

  void updateUserInfo(data) async {
    // List keys = data.keys.toList();
    // String firtKey = keys[0];
    // switch (firtKey) {
    //   case "name":
    //     userInfo.value.name = data[firtKey];
    //     break;
    //   case "description":
    //     userInfo.value.description = data[firtKey];
    //     break;
    //   case "university":
    //     userInfo.value.university = data[firtKey];
    //     break;
    //   case "birthday":
    //     userInfo.value.birthday = data[firtKey];
    //     break;
    //   case "cityStringArr":
    //     userInfo.value.city = data[firtKey];
    //     userInfo.value.province = data[firtKey];
    //     break;
    //   case "gender":
    //     userInfo.value.gender = data[firtKey];
    //     break;
    //   case "avatarUrl":
    //     userInfo.value.avatarUrl = data[firtKey];
    //     break;
    //   case "bgUrl":
    //     userInfo.value.bgUrl = data[firtKey];
    //     break;
    // }
    try {
      await UserService.updateUserInfo(data);
      ToastUtils.showToast("保存成功");
      getUserInfo();
    } catch (error) {
      ToastUtils.showToast(error['retdesc']);
    }
  }
}
